package com.psychologyCommunity.Community.Contoller.interceptor;

import com.psychologyCommunity.Community.entity.User;
import com.psychologyCommunity.Community.service.DataService;
import com.psychologyCommunity.Community.util.HostHolder;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class DataInterceptor implements HandlerInterceptor {
    @Autowired
    private DataService dataService;

    @Autowired
    private HostHolder hostHolder;

    //在请求之初统计就行了 ，在这只是做个统计  记录一次，统计完 请求还是继续执行 最后还得给它返回true

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
       //统计UV
        String remoteHostIp = request.getRemoteHost();
        dataService.recordUV(remoteHostIp);

        //统计DAU
        User user = hostHolder.getUser();
        if(user!=null){
            dataService.recordDAU(user.getId());
        }
        return true;
    }
}
